package com.sismul.model;

import java.util.ArrayList;
import java.util.List;

import com.sismul.MySQLiteHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WordsDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_WORD, MySQLiteHelper.COLUMN_COUNT };

	public WordsDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Word createWord(String keyword) {
		ContentValues values = new ContentValues();
		Word word;
		
		if ((word = get(keyword)) == null) { // no record
			values.put(MySQLiteHelper.COLUMN_WORD, keyword);
			values.put(MySQLiteHelper.COLUMN_COUNT, 1);
			long insertId = database.insert(MySQLiteHelper.TABLE_WORDS, null,
					values);
			Cursor cursor = database.query(MySQLiteHelper.TABLE_WORDS,
					allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
					null, null, null);
			cursor.moveToFirst();
			Word newWord = cursorToWord(cursor);
			cursor.close();
			return newWord;
		} else {
			Log.e("count-b",""+word.getCount());
			updateWord(word);
			
			Word newWord = get(word.getId());
			Log.e("count-a",""+newWord.getCount());
		    
			return newWord;
		}
	}

	public int updateWord(Word word) {
		ContentValues values = new ContentValues();
	    values.put(MySQLiteHelper.COLUMN_COUNT, (word.getCount() + 1));
	    
	    // updating row
	    return database.update(MySQLiteHelper.TABLE_WORDS, values, MySQLiteHelper.COLUMN_ID + " = " + word.getId(),
	            null);
	}
	
	public void deleteWord(Word word) {
		long id = word.getId();
		System.out.println("Word deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_WORDS, MySQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	public Word get (String keyword) {
		List<Word> words = new ArrayList<Word>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_WORDS, allColumns, MySQLiteHelper.COLUMN_WORD + " LIKE '" + keyword + "'", null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Word word = cursorToWord(cursor);
			words.add(word);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return (words.size() > 0 ? words.get(0) : null);
	}
	
	public Word get (long id) {
		List<Word> words = new ArrayList<Word>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_WORDS, allColumns, MySQLiteHelper.COLUMN_ID + " = " + id + "", null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Word word = cursorToWord(cursor);
			words.add(word);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return (words.size() > 0 ? words.get(0) : null);
	}
	
	public List<Word> getAllWords() {
		List<Word> words = new ArrayList<Word>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_WORDS,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Word word = cursorToWord(cursor);
			words.add(word);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return words;
	}

	private Word cursorToWord(Cursor cursor) {
		Word word = new Word();
		word.setId(cursor.getLong(0));
		word.setWord(cursor.getString(1));
		word.setCount(cursor.getInt(2));
		return word;
	}
}
